package com.taotaojs.util.reflect;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: TaoTaojs
 * @Date: 2019/4/24 16:51
 * @Description:
 * 将属性名转换为get/set方法名
 * @关联类:
 */
public class MethodUtil {
    private static final String SET_PREFIX = "set";
    private static final String GET_PREFIX = "get";
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    private MethodUtil(){}

    /**
     * 首字母变成大写
     * @param name
     * @return
     */
    private static String capitalize(String name) {
        if (name == null || name.length() == 0) {
            return name;
        }
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    /**
     * 获取set方法名
     * @param propertyName
     * @return
     */
    public static String setMethodName(String propertyName) {
        return SET_PREFIX + capitalize(propertyName);
    }

    /**
     * 获取get方法名
     * @param propertyName
     * @return
     */
    public static String getMethodName(String propertyName) {
        return GET_PREFIX + capitalize(propertyName);
    }

    /**
     * 根据属性名称获取表列名称
     * @param propertyName
     * @return
     */
    public static String getColnumName(String propertyName){
        Matcher matcher = humpPattern.matcher(propertyName);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


}
